<?php

namespace app\common\model;

use think\Session;
use think\Model;
use think\Loader;
use extend\Tree;

class BackendMenuModel extends Model
{
    protected $pk = 'id';
    protected $table = 'basic_backend_menu';

    protected static function init()
    {
        // GoodsModel::beforeInsert(function ($GoodsModel) {
        // 	$GoodsModel->shop_price = yuanToFen($GoodsModel->shop_price);
        // 	$GoodsModel->market_price = yuanToFen($GoodsModel->market_price);
        // });
    }

    public function getAllMenu($isShow = -1){
        //获取超级用户
        $acceptUser = Session::get("acceptUser");
        $backendUser = Session::get("backendUser");
        if (!in_array($backendUser['id'], $acceptUser)) {
            $menuList = $this->getAllUserGorupMenuData($backendUser['user_group_id']);
        } else {
            $menuList = $this->getAllMenuData();
        }
        return $menuList;
    }

    //获取所有的菜单
    public function getAllMenuData($isShow = -1)
    {
        $where['status'] = ['neq', 111];
        if ($isShow !== -1) {
            $where['is_show'] = $isShow;
        }

        $menus = self::where($where)->order("sort asc")->select();
        $menus = empty($menus) ? [] : collection($menus)->toArray();
        foreach ($menus as $key => $menu) {
            $menus[$key]['is_post'] = 1;
            $menus[$key]['is_accept'] = 1;
        }

        return $menus;
    }

    //获取权限组菜单
    public function getAllUserGorupMenuData($userGroupId, $isShow = -1)
    {
        $where = [
            'm.status' => ['neq', 111],
            'r.status' => ['neq', 111],
            'r.user_group_id' => $userGroupId
        ];

        if ($isShow !== -1) {
            $where['m.is_show'] = $isShow;
        }

        $menus = self::field("m.id,m.parent_id,m.menu_name,m.url,r.param,r.is_post,r.is_accept,m.icon")->where($where)
            ->alias("m")
            ->join("basic_backend_rule r", "m.id = r.menu_id")
            ->order("m.sort asc,m.id")
            ->select();
        $menus = empty($menus) ? [] : collection($menus)->toArray();

        return $menus;
    }


    //获取后台菜单列表
    public function getMenuList()
    {
        $menuList = $this->getAllMenu();
        
        Loader::import('Tree', EXTEND_PATH);
        $Tree = new Tree();
        $Tree->init($menuList, 'menu_name');
        $treelist = $Tree->get_tree_list();

        // dump( $treelist );die();
        return $treelist;
    }

    //获取后台菜单数组（children）
    public function getMenuAarray()
    {
        $menuList = $this->getAllMenuData();
        Loader::import('Tree', EXTEND_PATH);
        $Tree = new Tree();
        $Tree->init($menuList);
        $treelist = $Tree->getTreeData();
        // dump( $treelist );die();
        return $treelist;
    }

    //获取单个菜单
    public function getMenuById($id)
    {
        return self::find($id);
    }

    //获取权限组的菜单
    public function getUserGroupMenu($userGroupId)
    {
        //获取超级用户
        $acceptUser = Session::get("acceptUser");
        $backendUser = Session::get("backendUser");
        if (!in_array($backendUser['id'], $acceptUser)) {
            $userGroupMenu = $this->getAllUserGorupMenuData($userGroupId, 1);
        } else {
            $userGroupMenu = $this->getAllMenuData(1);
        }


        Loader::import('Tree', EXTEND_PATH);
        $Tree = new Tree();
        $Tree->init($userGroupMenu, 'menu_name');
        $treelist = $Tree->getTreeData(0);
        $treelist = $this->getElementMenu($treelist);

        return $treelist;
    }


    //删除
    public function deleteAll($id)
    {
        if (!is_array($id)) {
            $id = [$id];
        }

        return self::where("id", "in", $id)->update([
            'status' => 111,
            'update_time' => time()
        ]);
    }

    //判断该菜单是否有子分类
    public function isChildren($id)
    {
        return self::where(["parent_id"=>$id,'status'=>['neq',111]])->count() > 0 ? true : false;
    }

    private function getElementMenu($userMenu)
    {
        if (empty($userMenu)) {
            return;
        }
        $html = "";
        foreach ($userMenu as $key => $menu) {
            if (empty($menu['children'])) {
                $html .= "<el-menu-item index='{$menu['id']}' v-on:click=\"handleSkipUrl('" . Url($menu['url']) . "','{$menu['menu_name']}',{$menu['id']})\"><i class=\"{$menu['icon']}\"></i>{$menu['menu_name']}</el-menu-item>";
            } else {
                $html .= "<el-submenu index='{$menu['id']}'>";
                $html .= "<template slot=\"title\"><i class=\"{$menu['icon']}\"></i>{$menu['menu_name']}</template>";
                $html .= $this->getElementMenu($menu['children']);
                $html .= "</el-submenu>";
            }
        }
        return $html;
    }



    public function changeSalt($id, $sort){
        return self::where("id", "eq", $id)->update([
            'sort' => $sort,
            'update_time' => time()
        ]);
    }
}
